package com.qf.action;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qf.model.User;
import com.qf.service.impl.UserServiceImpl;



public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private UserServiceImpl userServiceImpl=new UserServiceImpl(); 
    
	public Login() {
        super();
       
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		String uname = request.getParameter("uname");
		String upwd=request.getParameter("upwd");// 获取密码
		
		
		
		
		User user=userServiceImpl.login(uname,upwd);
		System.out.println(user);
	    String uname1 = URLEncoder.encode(request.getParameter("uname"), "utf-8");

		Cookie c=new Cookie("uname", uname1);//记住姓名
		Cookie c1=new Cookie("upwd",upwd);//记住密码
		
		if(user!=null){
			response.addCookie(c);
			response.addCookie(c1);
			request.getSession().setAttribute("user", user);
			
			request.getRequestDispatcher("/jsp/index.jsp").forward(request, response);
			
			
		}else{
			request.setAttribute("error","用户名或密码错误");
			request.setAttribute("uname", uname);
			request.setAttribute("upwd", upwd);
			
		
			//request.getRequestDispatcher("/login.jsp").forward(request, response);
			
		}
		
		
		
		
		
		
		
			
		
	
	
	
	
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
